using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.EntityFrameworkCore;

namespace YayiMgr.Ability.CommonApp
{
    public static class HttpContextExtensions
    {
        // public static async Task<QueryListDto<T1>> CreateQueryListDto<T0, T1>(IQueryable<T0> DataQry, QueryByPage page)
        // {            
        //     int total = await DataQry.CountAsync();
        //     List<T0> list = await DataQry.ToListAsync();//.OrderBy(p=>p.Account).Skip((page.PageIndex-1)*page.PageSize).Take(page.PageSize).ToListAsync();
        //     List<T1> Data = IModelMapper.Map<List<T0>, List<T1>>(list);
        //     QueryListDto<T1> listDto = QueryListDto<T1>.Create(Data, "成功", total, page.PageIndex, page.PageSize);
        //     return listDto;


        //     // return new QueryListDto<T>{
        //     //     Data = data,
        //     //     Message = message,
        //     //     Total = total,
        //     //     PageCount = total / psize + ((total % psize) == 0 ? 0 : 1),
        //     //     PageIndex = pindex,
        //     //     PageSize = psize,
        //     //     Code = code
        //     // };
        // }

        /// <summary>
        /// AutoMapper修改实例时null字段不修改
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dest"></param>
        /// <param name="src"></param>
        /// <param name="ignoreFields">要忽略的字段，大小写必须一致</param>
        public static void UpdateEntity<T>(this T dest, T src, List<string> ignoreFields = null)
        {
            foreach (var property in src.GetType().GetProperties())
            {
                if(ignoreFields!=null && ignoreFields.Contains(property.Name))
                    continue;
                var value = property.GetValue(src);
                if (value != null)
                {
                    var entityProperty = dest.GetType().GetProperty(property.Name);
                    if (entityProperty != null && entityProperty.CanWrite)
                    {
                        entityProperty.SetValue(dest, value);
                    }
                }
            }
        }

    }
}